package com.archstoday.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

import com.archstoday.BaseApp;
import com.archstoday.R;
import com.archstoday.common.AsRes;
import com.archstoday.common.SpfUtils;
import com.archstoday.entity.UpdateEntity;
import com.archstoday.utils.OAuthUtils;
import com.archstoday.utils.ToolUtils;
import com.base.http.common.BaseHttpClient;
import com.base.utils.MySharedPreferencesMgr;
import com.base.utils.StringUtil;

/**
 * 设置
 */
public class SettingActivity extends AsScrollBaseActivity {

    private TextView mTitleTv;
    private TextView mVersionTv, mLoginOut;

    private String mVersion;

    @Override
    protected void initView(Bundle bundle) {
        setContentView(R.layout.activity_setting);
        mTitleTv = (TextView) findViewById(R.id.title_tv);
        mVersionTv = (TextView) findViewById(R.id.user_version_tv);
        mLoginOut = (TextView) findViewById(R.id.login_out_btn);
    }

    @Override
    protected void initData(Bundle bundle) {
        mTitleTv.setText(R.string.user_setting);
        if (!StringUtil.isEmpty(MySharedPreferencesMgr.getString(SpfUtils.USER_ID, ""))) {
            mLoginOut.setVisibility(View.VISIBLE);
        } else {
            mLoginOut.setVisibility(View.GONE);
        }

        mVersion = getVersion();
        mVersionTv.setText("V" + mVersion);
    }

    @Override
    protected void initListener() {
        setOnClickListener(R.id.back_btn);
        setOnClickListener(R.id.user_version_btn);
        setOnClickListener(R.id.login_out_btn);
    }

    @Override
    public void treatClickEvent(int id) {
        switch (id) {
            case R.id.back_btn:
                appFinish();
                break;
            case R.id.user_version_btn:
                upDate();
                break;
            case R.id.login_out_btn:
                getEvent(BaseApp.E_LOGOUT, BaseApp.PAGE_TYPE_LOGOUT);
                MySharedPreferencesMgr.setString(SpfUtils.USER_ID, "");
                MySharedPreferencesMgr.setString(SpfUtils.USER_TOKEN, "");
                mLoginOut.setVisibility(View.GONE);

                OAuthUtils.getInstance().cleanUserInfo();
                break;
        }
    }

    private void upDate() {
        initParameter();
        mParams.put("app_id", BaseApp.getDeviceID(mContext));
        mParams.put("version_id", mVersion.substring(0, mVersion.lastIndexOf(".")));
        mParams.put("version_mini", Integer.valueOf(mVersion.substring(mVersion.lastIndexOf(".") + 1)));
        mParams.put("type", "android");
        mParams.put("channel", BaseApp.mChannel);

        sendEncryptPostRequest(AsRes.getInstance().getUrl(AsRes.GET_UPDATE),
                AsRes.GET_UPDATE, mParams, UpdateEntity.class);
    }

    @Override
    protected void onSuccess(String content, BaseHttpClient client, Object parse) {
        if (client.getUrlIdentifier() == AsRes.GET_UPDATE) {
            final UpdateEntity entity = (UpdateEntity) parse;
            if (ToolUtils.isSuccess(entity.getStatus())) {
                if (entity.getData() == null
                        || StringUtil.isEmpty(entity.getData().getApkUrl())) {
                    showToast(R.string.update_toast);
                } else {
                    if (entity.getData().getIsForced() == 1) {
                        AlertDialog dialog = new AlertDialog.Builder(mContext)
                                .setTitle(R.string.update_title)
                                .setMessage(entity.getData().getUpgradePoint())
                                .setNegativeButton(R.string.cancel_btn, new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {

                                    }
                                })
                                .setPositiveButton(R.string.submit_btn, new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        if (ToolUtils.isPermission(mContext)) {
                                            ToolUtils.intoDownloadManager(mContext,
                                                    entity.getData().getApkUrl(), false);
                                        } else {
                                            ToolUtils.getAppDetailSettingIntent(mContext);
                                        }
                                    }
                                })
                                .create();
                        dialog.show();
                    } else if (entity.getData().getIsForced() == 2) {
                        AlertDialog dialog = new AlertDialog.Builder(mContext)
                                .setTitle(R.string.update_title)
                                .setMessage(entity.getData().getUpgradePoint())
                                .setPositiveButton(R.string.submit_btn, new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        if (ToolUtils.isPermission(mContext)) {
                                            showToast(R.string.download_toast);
                                            new Handler().postDelayed(new Runnable() {
                                                @Override
                                                public void run() {
                                                    ToolUtils.intoDownloadManager(mContext,
                                                            entity.getData().getApkUrl(), true);
                                                }

                                            }, 150);
                                        } else {
                                            ToolUtils.getAppDetailSettingIntent(mContext);
                                        }
                                    }
                                })
                                .create();
                        dialog.setCancelable(false);
                        dialog.show();
                    }
                }
            } else {
                showToast(entity.getMsg());
            }
        }
    }
}